<?php
/* SVN FILE: $Id: coaches_controller.php 329 2010-07-21 11:52:15Z gregory.rome $ */
/**
 * Coaches Controller.
 *
 * Addition, deletion and edition are administrators tasks.
 *
 * cBBweb : Digital Blood Bowl league interface (http://code.google.com/p/cbbweb/)
 * Copyright 2009-2010, Grégory Romé.
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright     Copyright 2009-2010, Grégory Romé.
 * @link          http://code.google.com/p/cbbweb/ cBBweb Project
 * @package       cbbweb
 * @subpackage    cbbweb.controllers
 * @version       $Revision: 329 $
 * @modifiedby    $LastChangedBy: gregory.rome $
 * @lastmodified  $Date: 2010-07-21 11:52:15 +0000 (Wed, 21 Jul 2010) $
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
 */

/**
 * Coaches Controller.
 *
 * @package       cbbweb
 * @subpackage    cbbweb.controllers
 */
class CoachesController extends AppController {

	var $name = 'Coaches';
	var $paginate = array (
		'Coach' => array (
		'contain' => array (
			'User',
			'StatisticsCoach')),
		'Team' => array (
			'contain' => array (
				'Race' => array ('LocalizedString'),
				'StatisticsTeam')));

	/* ------------------------------------------------------------ */
	/**
	 * Index
	 */
	function index() {
		$coaches = $this->paginate('Coach');
		if (isset($this->params['requested'])) {
			return $coaches;
		} else {
			$this->set('coaches', $coaches);
		}
	}

	/* ------------------------------------------------------------ */
	/** View
	 *
	 */
	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Coach.', true));
			$this->redirect(array('action'=>'index'));
		}

		$this->Coach->recursive = 1;
		$coach = $this->Coach->read(null,$id);


		if(!$this->championship) {
			$team_list = $this->Coach->Team->find('list', array(
				'fields' => array('Team.ID'),
				'conditions' => array('Team.idCoach' => $id)
			));
		} else {
			$this->Session->setFlash(__('Deselect the championship to display all teams.', true));
			$team_list = $this->Coach->Team->find('list', array(
				'fields' => array('Team.ID'),
				'conditions' => array(
					'Team.idCoach' => $id,
					'Team.ID' => $this->current['Teams']
				)
			));
		}

		$teams = $this->paginate('Team',array('Team.ID' => $team_list));

		$this->set('coach', $coach);
		$this->set('teams', $teams);
	}

	/* ------------------------------------------------------------ */
	/** Add
	 *
	 */
	function add()
	{

		if (!empty($this->data['Coach'])) {
			$this->Coach->create();
			$this->data['Coach']['strTag_Voice'] = "";
			$this->data['Coach']['strTag_Head'] = "";

			if ($this->Coach->save($this->data)) {
				$this->data['Coach']['ID'] = $this->Coach->getId();

				foreach ($this->data['Coach']['Team'] as $team_id )
				{
					$this->Coach->Team->recursive = -1;
					$team = $this->Coach->Team->read(null, $team_id);
					if(empty($team['Team']['idCoach'])) {
						$team['Team']['idCoach'] = $this->data['Coach']['ID'];
					}else
					{
						$this->Session->setFlash(__('Team already coached', true));
					}
					$this->Coach->Team->save($team);
				}
				$this->Session->setFlash(__('The Coach has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Coach could not be saved. Please, try again.', true));
			}
		}

		if(!$this->championship) {
			$teams = $this->Coach->Team->find('list', array (
				'fields' => array ('Team.ID', 'Team.strName'),
			));
		} else {
			$teams = $this->current['Team'];
		}
		$this->set(compact('teams'));
	}

	/* ------------------------------------------------------------ */
	/** Edit
	 *
	 */
	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Coach', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data['Coach'])) {
			if ($this->Coach->save($this->data)) {
				$this->data['Coach']['ID'] = $this->Coach->getId();

				foreach ($this->data['Coach']['Team'] as $team_id )
				{
					$this->Coach->Team->recursive = -1;
					$team = $this->Coach->Team->read(null, $team_id);
					if(empty($team['Team']['idCoach'])) {
						$team['Team']['idCoach'] = $this->data['Coach']['ID'];
					}else
					{
						$this->Session->setFlash(__('Team already coached', true));
					}
					$this->Coach->Team->save($team);
				}

				$this->Session->setFlash(__('The Coach has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Coach could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Coach->read(null, $id);
		}

		if(!$this->championship) {
			$teams = $this->Coach->Team->find('list', array (
				'fields' => array ('ID', 'strName')
			));
		} else {
			$teams = $this->current['Team'];
		}
		$this->set(compact('teams'));
	}

	/* ------------------------------------------------------------ */
	/** Delete
	 *
	 */
	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Coach', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Coach->delete($id)) {
			$this->Session->setFlash(__('Coach deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}

}
?>
